//gw.db('login.sqlite').open().exeSQL('CREATE TABLE login(host CHAR(10) NOT NULL,user CHAR(20) NOT NULL,islog INTEGER default 1,info TEXT,PRIMARY KEY (host,user))').close()
//gw.db('login.sqlite').open().getData("select * from login ",[0,0,1,0]);
//gw.db('login.sqlite').open().exeSQL("delete from login ").close()
/*
	CREATE TABLE login(
		host CHAR(10) NOT NULL,
		user CHAR(20) NOT NULL,
		islog INTEGER default 1,
		info TEXT,
		PRIMARY KEY (host,user)
	)
*/


(function (jQuery){


	jQuery.net={};
	var db=gw.db('login.sqlite');
	
	
	jQuery.net.load=function(hostname,load,useInfos){
		this.hostname=hostname;
		this.useInfos=useInfos||gw.cache[hostname].useInfos;
		this.load=load;	
	}
	jQuery.net.load.prototype={
		'_addall':function(){
			for(var i in this.useInfos){
				var useInfo=this.useInfos[i];
				this.load.call(useInfo);
			}
			return this;
		}
	}
	

	jQuery.net.cache=function(hostname,init,_default){
		this.hostname=hostname;
		this.useInfos={};
		this.init=init;
		_default=_default||{};
		this._default=jQuery.extend({'islog':1},_default);		
	}
	jQuery.net.cache.prototype={
		'_add':function(opt){
			if(jQuery.isArray(opt)){
				var re={'userId':opt[0]};
				if(opt.length>=2){
					re.passWord=opt[1];
				}
				if(opt.length==3){
					re.name=opt[2];
				}
				opt=re;
			}
			opt=jQuery.extend({},this._default,opt);
			this.useInfos[opt.userId]=this.init.call(new jQuery.net.useInfo(opt));
			return this;
		},
		'_addAry':function(ary){
			var me=this;
			ary.forEach(function(v){
				me._add({'userId':v});
			});	
			return this;
		},
		'toDB':function(){
			db.open()
			for(var i in this.useInfos){
				//console.log("REPLACE INTO login (host, user,islog,info) VALUES ('"+this.hostname+"','"+i+"',"+this.useInfos[i].islog+", '"+JSON.stringify(this.useInfos[i])+"'); ")
				db.exeSQL("REPLACE INTO login (host, user,islog,info) VALUES ('"+this.hostname+"','"+i+"',"+this.useInfos[i].islog+", '"+JSON.stringify(this.useInfos[i])+"'); ");
			}
			db.close();
			return this;
		}
	}
		
	jQuery.net.useInfo=function(opt){
		var re=/[^\x00-\xff]/;
		if(!!!opt.name){
			opt.name=opt.userId;
		}
		jQuery.extend(this,opt);
		this.userId2=re.test(opt.userId)?gw.convert(opt.userId):opt.userId;
	}
	
	
	jQuery.net.expand=function(expand){
		for(var i in expand){
			if(jQuery.isFunction(expand[i])){
				this[i]=expand[i]();
			}else{
				this[i]=expand[i];
			}
		}
	}
	
	jQuery.net.expand.prototype={
		'toString':function(){
			var html="";
			for(var i in this){
				html+="&"+i+"="+this[i];
			}
			return html.substr(1);
		}
	}
	
	
	
	jQuery.net.panel={
		'_panel':function (x,button){

			if(jQuery("#wgu_tool").length==0){
				jQuery('<div id="wgu_tool" style="z-index:99999;bottom:0px;border-width: 1px;position: fixed;border-style: solid;border-radius: 11px 11px 0px 0px;padding:5px;background-color:White;">'
					+'<div>welcome to my tools! welcome to my tools! </div>'
					+'</div>').appendTo('body');
			}else{
				jQuery("#wgu_tool").html('<div id="wgu_tool" style="z-index:99999;bottom:0px;border-width: 1px;position: fixed;border-style: solid;border-radius: 11px 11px 0px 0px;padding:5px;background-color:White;">'
					+'<div>welcome to my tools! welcome to my tools! </div>'
					+'</div>');
			}				
			var temp=jQuery('<div></div>').appendTo('#wgu_tool');
			button.forEach(function(v,i){
				jQuery('<button>'+v.name+'</button>').appendTo(temp).bind('click',v);
			});
		
			return this;
		}
	}
})(___jQuery)